<?php
namespace app\api\controller;

use app\api\controller;
use think\Db;


class Charge extends Base
{   
     //用户使用记录
    public function usagelog(){
  
        $user_charge  = model('charge')->getUserCharge($this->userId);
        
        $user_charge_list = array();
        if(is_array($user_charge)){
            foreach ($user_charge as $key => $value) {
                if($value['duration'] > 0){
                    $user_charge_list[$key]['end_time']   = date('Y-m-d H:i',$value['end_time']);
                    $user_charge_list[$key]['cue_name']   = '力杆-野豹';
                    $user_charge_list[$key]['use_time']   = formatDuration($value['duration']);
                    $user_charge_list[$key]['club_name']  = $value['club_name'];
                    $user_charge_list[$key]['amount']     = $value['amount'];
                    $user_charge_list[$key]['cue_number'] = $value['cue_number'];
                }
              
            }
        }

        $this->success("请求成功!", $user_charge_list);

    }
    

    //添加订单信息
    public function addCharge(){

        $order_info = $this->request->param();


        $out_trade_no = date('YmdHis',time()).getRandCode(6);//商户的订单号
       
    
        if($data['result'] = model('Charge')->pay_place_order($this->userId,$order_info['amount'],$out_trade_no,$this->user['openid'])){
            
            $lock_info = Db::name('lock')
                      ->alias('l')
                      ->join('__BOX__ b','l.box_id = b.id','left')
                      ->join('__CLUB__ c','b.club_id = c.id','left')
                      ->field('l.number as lock_number ,c.id as club_id ,c.name as club_name')
                      ->where('l.id',$order_info['lock_id'])
                      ->find();

            $order_info['start_time']   = time();
            $order_info['out_trade_no'] = $out_trade_no;
            $order_info['lock_number']  = $lock_info['lock_number'];
            $order_info['club_id']      = $lock_info['club_id'];
            $order_info['club_name']    = $lock_info['club_name'];
            $order_info['user_id']      = $this->userId;
            
            
            if($data['charge_id']   = Db::name('charge_log')->insertGetId($order_info)){
                $data['lock_id']    = $order_info['lock_id'];
                $data['cue_number'] = $order_info['cue_number']; 
                $this->success("下单成功!", $data);
            }
        }

        $this->error('下单失败！');

    }

    //用户支付完成修改订单信息
    public function updateCharge(){

        $data = $this->request->param();
        
        $user_id    = $this->userId;
        $charge_id = $data['charge_id'];
        $lock_id   = $data['lock_id'];

        //修改订单状态为已支付
        Db::name('charge_log')->where('id',$charge_id)->where('user_id',$user_id)->update(['is_pay' => 1]);
        //球杆状态表中添加用户id，表示用户正在使用
        $time    = time();
        $user_id = $this->userId;
        Db::name('cue_status')->where('cue_number',$data['cue_number'])->update(['user_id' => $user_id,'update_time' => $time]);
        //修改红包状态为已使用
        $charge_info = Db::name('charge_log')->where('id',$charge_id)->field('user_redpacket_id,deposit,out_trade_no')->find();
        
        //用户缴纳押金
        if($charge_info['deposit'] > 0){
            $deposit      = $charge_info['deposit'];
            $out_trade_no = $charge_info['out_trade_no'];
            Db::name('user')->where('id',$user_id)->update(['deposit' => $deposit,'out_trade_no'=>$out_trade_no]);
        }
        

        Db::name('user_redpacket')->where('id',$charge_info['user_redpacket_id'])->update(['status'=>1,'use_time'=>$time]);
        //通知远程柜子开锁
        $lock_info = Db::name('lock')->where('id',$lock_id)->field('control_board_number,lock_number')->find();

        $this->request($lock_info['control_board_number'],$lock_info['lock_number'],$data['cue_number']);

        $this->success("下单成功!",['cue_number' => $data['cue_number']]);
    }

       //开锁的接口
    private function request($equipment,$code,$cue_number){
        $data['equipment']  = $equipment;
        $data['code']       = $code;
        $data['Cue_Number'] = $cue_number;
  
        $url = "http://47.93.103.53:9001/WebService1.asmx/MQTTService";
        // $url    = "http://47.93/WebService1.asmx/MQTTService";
        $result = httpRequest($url,'POST',$data);
      
        return $result;
    }
}
